package com.pubinfo.passbook.mdm.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pubinfo.passbook.common.entity.*;
import com.pubinfo.passbook.common.model.vo.mdm.MdmResult;
import com.pubinfo.passbook.common.service.master.*;
import com.pubinfo.passbook.mdm.service.MDMSendDataService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class MDMSendDataServiceImpl implements MDMSendDataService {
  @Autowired
  ITMdmSupplierService supplierService;
  @Autowired
  ITMdmAccountingTitleService accountingTitleService;
  @Autowired
  ITMdmAltAccountService altAccountService;
  @Autowired
  ITMdmBankAccountService accountService;
  @Autowired
  ITMdmBankService bankService;
  @Autowired
  ITMdmCompanyService companyService;
  @Autowired
  ITMdmContractService contractService;
  @Autowired
  ITMdmCostCenterGroupService costCenterGroupService;
  @Autowired
  ITMdmCostCenterService costCenterService;
  @Autowired
  ITMdmProfitCenterGroupService centerGroupService;
  
  @Autowired
  TMdmSupplierBankService supplierBankService;
  @Autowired
  ITMdmProfitCenterService centerService;
  
  @SneakyThrows
  @Override
  public String account(String messageJson, String baseinfoJson) {
    log.info("参数:" + messageJson);
    long startTime = System.currentTimeMillis();
    TMdmAccountingTitle object = str2Object(messageJson, TMdmAccountingTitle.class);
  
    if (accountingTitleService.saveOrUpdate(object)) {
      log.info("执行时间:" + (System.currentTimeMillis() - startTime));
      return MdmResult.success();
    }
    return MdmResult.error("调用失败");
  }
  
  @Override
  public String altAccount(String messageJson, String baseinfoJson) {
    log.info("参数:" + messageJson);
    long startTime = System.currentTimeMillis();
    TMdmAltAccount object = str2Object(messageJson, TMdmAltAccount.class);
    //合并主键
    object.setAltId(object.getKtopl() + object.getHkont() + object.getAbwhk() + object.getKrzky());
    if (altAccountService.saveOrUpdate(object)) {
      log.info("执行时间:" + (System.currentTimeMillis() - startTime));
      return MdmResult.success();
    }
    return MdmResult.error("调用失败");
  }
  
  @Override
  public String bankac(String messageJson, String baseinfoJson) {
    log.info("参数:" + messageJson);
    long startTime = System.currentTimeMillis();
    TMdmBankAccount object = str2Object(messageJson, TMdmBankAccount.class);
    if (accountService.saveOrUpdate(object)) {
      log.info("参数:" + messageJson);
      log.info("执行时间:" + (System.currentTimeMillis() - startTime));
      return MdmResult.success();
    }
    return MdmResult.error("调用失败");
  }
  
  @Override
  public String bankid(String messageJson, String baseinfoJson) {
    log.info("参数:" + messageJson);
    long startTime = System.currentTimeMillis();
    TMdmBank object = str2Object(messageJson, TMdmBank.class);
    if (bankService.saveOrUpdate(object)) {
      log.info("执行时间:" + (System.currentTimeMillis() - startTime));
      return MdmResult.success();
    }
    return MdmResult.error("调用失败");
  }
  
  @Override
  public String companycode(String messageJson, String baseinfoJson) {
    log.info("参数:" + messageJson);
    long startTime = System.currentTimeMillis();
    TMdmCompany object = str2Object(messageJson, TMdmCompany.class);
    //合并主键
    object.setBukrsId(object.getKunnr() + object.getBukrs());
    if (companyService.saveOrUpdate(object)) {
      log.info("执行时间:" + (System.currentTimeMillis() - startTime));
      return MdmResult.success();
    }
    return MdmResult.error("调用失败");
  }
  
  @Override
  public String costCenter(String messageJson, String baseinfoJson) {
    log.info("参数:" + messageJson);
    long startTime = System.currentTimeMillis();
    TMdmCostCenter object = str2Object(messageJson, TMdmCostCenter.class);
    //合并主键
    object.setCenterId(object.getKhinr() + object.getKostl());
    if (costCenterService.saveOrUpdate(object)) {
      log.info("执行时间:" + (System.currentTimeMillis() - startTime));
      return MdmResult.success();
    }
    return MdmResult.error("调用失败");
  }
  
  @Override
  public String costCenterGro(String messageJson, String baseinfoJson) {
    log.info("参数:" + messageJson);
    long startTime = System.currentTimeMillis();
    TMdmCostCenterGroup object = str2Object(messageJson, TMdmCostCenterGroup.class);
    if (costCenterGroupService.saveOrUpdate(object)) {
      log.info("执行时间:" + (System.currentTimeMillis() - startTime));
      return MdmResult.success();
    }
    return MdmResult.error("调用失败");
  }
  
  @Override
  public String profitCenter(String messageJson, String baseinfoJson) {
    log.info("参数:" + messageJson);
    long startTime = System.currentTimeMillis();
    TMdmProfitCenter object = str2Object(messageJson, TMdmProfitCenter.class);
    if (centerService.saveOrUpdate(object)) {
  
      log.info("执行时间:" + (System.currentTimeMillis() - startTime));
      return MdmResult.success();
    }
    return MdmResult.error("调用失败");
  }
  
  @Override
  public String proCenterGro(String messageJson, String baseinfoJson) {
    log.info("参数:" + messageJson);
    long startTime = System.currentTimeMillis();
    TMdmProfitCenterGroup object = str2Object(messageJson, TMdmProfitCenterGroup.class);
    if (centerGroupService.saveOrUpdate(object)) {
  
      log.info("执行时间:" + (System.currentTimeMillis() - startTime));
      return MdmResult.success();
    }
    return MdmResult.error("调用失败");
  }
  
  @Override
  public String opSupplierCreate(String messageJson, String baseinfoJson) {
    log.info("供应商参数:" + messageJson);
    long startTime = System.currentTimeMillis();
    TMdmSupplier object = str2Object(messageJson, TMdmSupplier.class);
    if (supplierService.saveOrUpdate(object)) {
      String listItems = object.getListitem2();
      if (StringUtils.isNotBlank(listItems)) {
        JSONArray array = JSON.parseArray(listItems);
        List<TMdmSupplierBank> bankList = array.toJavaList(TMdmSupplierBank.class);
        supplierBankService.saveBatch(bankList);
      }
  
      log.info("执行时间:" + (System.currentTimeMillis() - startTime));
      return MdmResult.success();
    }
    return MdmResult.error("调用失败");
  }
  
  private <T> T str2Object(String str, Class<T> cla) {
    T t = null;
    if (StringUtils.isNotBlank(str)) {
      try {
        JSONObject jsonObject = JSON.parseObject(str);
        t = JSONObject.toJavaObject(jsonObject, cla);
      } catch (Exception e) {
        log.error("解析dubbo服务参数失败:" + str, e);
      }
    }
    return t;
  }
  
  @Override
  public String contractData(String messageJson, String baseinfoJson) {
    log.info("参数:" + messageJson);
    long startTime = System.currentTimeMillis();
    TMdmContract object = str2Object(messageJson, TMdmContract.class);
    if (contractService.saveOrUpdate(object)) {
  
      log.info("执行时间:" + (System.currentTimeMillis() - startTime));
      return MdmResult.success();
    }
    return MdmResult.error("调用失败");
  }
}
